home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Demo / cwilib / cwilib.py next >
Encoding:
Text File  |  2000-06-23  |  6.5 KB  |  214 lines

  1. # Interface to the interactive CWI library catalog.
  2.  
  3. import sys
  4. import stdwin
  5. from stdwinevents import *
  6. import select
  7. import telnetlib
  8. import vt100win
  9. from form import Form
  10.  
  11.  
  12. # Main program
  13.  
  14. def main():
  15.     vt = vt100win.VT100win()
  16.     #
  17.     host = 'biefstuk.cwi.nl'
  18.     tn = telnetlib.Telnet(host, 0)
  19.     #
  20.     try:
  21.         vt.send(tn.read_until('login: ', 10))
  22.         tn.write('cwilib\r')
  23.         #
  24.         vt.send(tn.read_until('Hit <RETURN> to continue...', 10))
  25.         tn.write('\r')
  26.         #
  27.         vt.send(tn.read_until('QUIT', 20))
  28.     except EOFError:
  29.         sys.stderr.write('Connection closed prematurely\n')
  30.         sys.exit(1)
  31.     #
  32.     define_screens(vt)
  33.     matches = vt.which_screens()
  34.     if 'menu' not in matches:
  35.         sys.stderr.write('Main menu does not appear\n')
  36.         sys.exit(1)
  37.     #
  38.     tn.write('\r\r')
  39.     vt.open('Progress -- CWI Library')
  40.     vt.set_debuglevel(0)
  41.     ui = UserInterface()
  42.     #
  43.     while 1:
  44.         try:
  45.             data = tn.read_very_eager()
  46.         except EOFError:
  47.             stdwin.message('Connection closed--goodbye')
  48.             break
  49.         if data:
  50.             print 'send...'
  51.             vt.send(data)
  52.             print 'send...done'
  53.             continue
  54.         event = stdwin.pollevent()
  55.         if event:
  56.             type, window, detail = event
  57.             if window == None and type == WE_LOST_SEL:
  58.                 window = ui.queryform.window
  59.                 event = type, window, detail
  60.             if type == WE_CLOSE:
  61.                 break
  62.             if window in ui.windows:
  63.                 ui.dispatch(type, window, detail)
  64.             elif window == vt.window:
  65.                 if type == WE_NULL:
  66.                     pass
  67.                 elif type == WE_COMMAND:
  68.                     if detail == WC_RETURN:
  69.                         tn.write('\r')
  70.                     elif detail == WC_BACKSPACE:
  71.                         tn.write('\b')
  72.                     elif detail == WC_TAB:
  73.                         tn.write('\t')
  74.                     elif detail == WC_UP:
  75.                         tn.write('\033[A')
  76.                     elif detail == WC_DOWN:
  77.                         tn.write('\033[B')
  78.                     elif detail == WC_RIGHT:
  79.                         tn.write('\033[C')
  80.                     elif detail == WC_LEFT:
  81.                         tn.write('\033[D')
  82.                     else:
  83.                         print '*** Command:', detail
  84.                 elif type == WE_CHAR:
  85.                     tn.write(detail)
  86.                 elif type == WE_DRAW:
  87.                     vt.draw(detail)
  88.                 elif type in (WE_ACTIVATE, WE_DEACTIVATE):
  89.                     pass
  90.                 else:
  91.                     print '*** VT100 event:', type, detail
  92.             else:
  93.                 print '*** Alien event:', type, window, detail
  94.             continue
  95.         rfd, wfd, xfd = select.select([tn, stdwin], [], [])
  96.  
  97.  
  98. # Subroutine to define our screen recognition patterns
  99.  
  100. def define_screens(vt):
  101.     vt.define_screen('menu', {
  102.               'title': ('search', 0, 0, 80,
  103.                         ' SEARCH FUNCTIONS  +OTHER FUNCTIONS '),
  104.               })
  105.     vt.define_screen('search', {
  106.               'title': ('search', 0, 0, 80, ' Search '),
  107.               })
  108.     vt.define_screen('shortlist', {'title': ('search', 0, 0, 80,
  109.               ' Short-list')})
  110.     vt.define_screen('showrecord', {
  111.               'title': ('search', 0, 0, 80, ' Show record '),
  112.               })
  113.     vt.define_screen('timelimit', {
  114.               'limit': ('search', 12, 0, 80, ' TIME LIMIT '),
  115.               })
  116.     vt.define_screen('attention', {
  117.               'BASE': ('copy', 0, 0, 0, 'search'),
  118.               'title': ('search', 10, 0, 80, ' ATTENTION ')})
  119.     vt.define_screen('syntaxerror', {
  120.               'BASE': ('copy', 0, 0, 0, 'attention'),
  121.               'message': ('search', 12, 0, 80, ' Syntax error'),
  122.               })
  123.     vt.define_screen('emptyerror', {
  124.               'BASE': ('copy', 0, 0, 0, 'attention'),
  125.               'message': ('search', 12, 0, 80,
  126.                           ' Check your input. Search at least one term'),
  127.               })
  128.     vt.define_screen('unsortedwarning', {
  129.               'BASE': ('copy', 0, 0, 0, 'attention'),
  130.               'message': ('search', 12, 0, 80,
  131.                           ' Number of records exceeds sort limit'),
  132.               })
  133.     vt.define_screen('thereismore', {
  134.               'BASE': ('copy', 0, 0, 0, 'showrecord'),
  135.               'message': ('search', 15, 0, 80,
  136.                  'There is more within this record. Use the arrow keys'),
  137.               })
  138.     vt.define_screen('nofurther', {
  139.               'BASE': ('copy', 0, 0, 0, 'showrecord'),
  140.               'message': ('search', 17, 0, 80, 'You cannot go further\.'),
  141.               })
  142.     vt.define_screen('nofurtherback', {
  143.               'BASE': ('copy', 0, 0, 0, 'showrecord'),
  144.               'message': ('search', 17, 0, 80,
  145.                           'You cannot go further back'),
  146.               })
  147.  
  148.  
  149. # Class to implement our user interface.
  150.  
  151. class UserInterface:
  152.  
  153.     def __init__(self):
  154.         stdwin.setfont('7x14')
  155.         self.queryform = QueryForm()
  156.         self.listform = ListForm()
  157.         self.recordform = RecordForm()
  158.         self.forms = [self.queryform, self.listform, self.recordform]
  159.         define_query_fields(self.queryform)
  160.         self.windows = []
  161.         for form in self.forms:
  162.             if form.formheight > 0:
  163.                 form.open()
  164.                 self.windows.append(form.window)
  165.  
  166.     def __del__(self):
  167.         self.close()
  168.  
  169.     def close(self):
  170.         for form in self.forms:
  171.             form.close()
  172.  
  173.     def dispatch(self, type, window, detail):
  174.         for form in self.forms:
  175.             if window == form.window:
  176.                 form.dispatch(type, detail)
  177.  
  178.  
  179. def define_query_fields(f):
  180.     f.define_field('name', 'Name auth./ed.', 1, 60)
  181.     f.define_field('title',  'Title', 4, 60)
  182.     f.define_field('shelfmark', 'Shelf mark', 1, 60)
  183.     f.define_field('class', 'Prim. classif.', 1, 60)
  184.     f.define_field('series', 'Series', 1, 60)
  185.     f.define_field('congress', 'Congr. pl./year', 1, 60)
  186.     f.define_field('type', 'Type', 1, 60)
  187.  
  188.  
  189. class QueryForm(Form):
  190.  
  191.     def __init__(self):
  192.         Form.__init__(self, 'Query form -- CWI Library')
  193.  
  194.     def dispatch(self, type, detail):
  195.         if type == WE_COMMAND and detail == WC_RETURN:
  196.             print '*** SUBMIT ***'
  197.         else:
  198.             Form.dispatch(self, type, detail)
  199.  
  200.  
  201. class ListForm(Form):
  202.  
  203.     def __init__(self):
  204.         Form.__init__(self, 'Short list -- CWI Library')
  205.  
  206.  
  207. class RecordForm(Form):
  208.  
  209.     def __init__(self):
  210.         Form.__init__(self, 'Record detail -- CWI Library')
  211.  
  212.  
  213. main()
  214.